package org.leumasjaffe.initiativeTracker.event;


import java.awt.event.ActionEvent;

import javax.swing.Action;

import org.leumasjaffe.initiativeTracker.Constants;
import org.leumasjaffe.initiativeTracker.operator.CharacterLinker;

import edu.wpi.heineman.xml.Message;
import edu.wpi.heineman.client.ServerAccess;

@SuppressWarnings("serial")
public class KillAction extends InitiativeTrackerAction implements Action {
	
	
	
	public KillAction(String text) {
		super(text);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String xmlString = Message.requestHeader() + "<killRequest game='" + Constants.id + "'>";
		
		for (int i = 0; i < list.size(); i++) {
			CharacterLinker c = list.get(i);
			if (c.isTarget()){
				c.tearDown();
				root.addDeath(i);
				System.out.println(c.getCharacter().getName() + " has perished!");
				list.remove(c);
				i--;
				
				xmlString += "<target id='" + c.getCharacter().getId() + "'/>";
				
				c = null;
			}
		}
		
		xmlString += "</killRequest></request>";
		if (xmlString.contains("target"))
			ServerAccess.getInstance().sendRequest(new Message(xmlString));
		
		root.rebuild();
		gui.repaint();
	}
	
	
}

